define(function(require, exports) {
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var popwin = require('@base/dialog').base;

	var AttachCampaign = popwin.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'title': '',
				'width': 800,
				'param': {
					mongoField: ['Name']
				}
			});

			this.$title = LANG('活动列表--渠道:');
			this.$data = {};
			this.Super('init', arguments);
		},
		afterBuild: function() {
			this.Super('afterBuild', arguments);
			var c = this.getConfig();
			var el = this.getDOM('body');
			el.append($([
				'<div pub-name="grid" pub-mod="@base/grid.baseNoDate"></div>'
			].join('')));

			var config = {
				'grid': {
					'url': 'campaign/list',
					'param': c.param,
					'cols': [
						{type: 'select'},
						{name:'_id',text:"ID", sort: true},
						{type: 'index', name: 'Name', width: 210}
					],
					'sub_field': '_id',
					'reqType': 'websocket',
					'opClick': true,
					'hasTab': false,
					'hasPager': true,
					'hasSearch': true,
					'hasExport': false,
					'hasAmount': false,
					'pager': {
						'size': 8
					}
				}
			};

			this.buildFromTemplate(config);
		},
		onOk: function() {
			var self = this;
			var campaign_id = this.$.grid.getSelectRowIds();
			if(campaign_id && campaign_id.length) {
				pubjs.mc.send('campaign/spotAppendFilter', {
					campaign_id: campaign_id
					,spot_id: self.$param.ids
					,status: 1
				}, function(err, data) {
					if(err) {
						pubjs.alert(err.message);
						return false;
					}
					pubjs.alert(LANG('操作成功'));
				})
			}

			self.hide();
			return false;
		},
		load: function() {
			this.$.grid.reload();
			return this;
		},
		setParam: function(param, replace) {
			this.$param = param;
			if(param.ChannelId) {
				this.$channelMap = this.$channelMap || pubjs.config('channelMap');
				this.setTitle(this.$title + this.$channelMap[param.ChannelId]);
			}
			this.$.grid.setParam(param.ChannelId, replace);
			return this;
		},
		reset: function() {
			this.$.grid.load();
			return this;
		}
	});
	exports.attachCampaign = AttachCampaign;

	// 编辑媒体广告位 by TBC
	var EditMediaSpot = popwin.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'width': 800,
				'height': 600,
				'url': 'massMedia/update',
				'view_model': {
					'mediaName': ''
				}
			});
			this.Super('init', arguments);
		},

		afterBuild: function() {
			this.Super('afterBuild', arguments);
			var el = this.getDOM('body');

			el.addClass('editMediaSpot');
			this.setTitle(LANG('编辑媒体广告位'));
			$([
				'<div class="head">',
					'<h2>'+ LANG('广告位信息') +'</h2>',
					'<div class="M-formSectionTitle">'+ LANG('填写说明：') +'</div>',
					'<ul class="M-formSectionDesc">',
						'<li>'+ LANG('必须先为广告位选择所属渠道、媒体和频道。') +'</li>',
						'<li>'+ LANG('每个广告位的属性有单价、类型、位置、尺寸，相当于用来标志广告位的关键字词，便于查找和定位广告位。') +'</li>',
						'<li>'+ LANG('系统会根据属性自动生成名称，建议不用修改。') +'</li>',
					'</ul>',
				'</div>',
				'<div class="content">',
					'<div class="M-formConGroup">',
						'<label class="group-label">'+ LANG('渠道：') +'</label>',
						'<div class="group-content">',
							'<span pub-mod="@base/common/drop.drop" pub-name="channel"/>',
							'<span pub-mod="@base/tip.desc" pub-name="channelTip" class="ml15"/>',
						'</div>',
					'</div>',
					'<div class="M-formConGroup">',
						'<label class="group-label">'+ LANG('媒体名称：') +'</label>',
						'<div class="group-content">',
							'<input type="text" ms-duplex="mediaName" class="w300">',
						'</div>',
					'</div>',
				'</div>'
			].join('')).appendTo(el);

			this.buildFromTemplate({
				'channel': {
					'url': 'adSubChannel/list',
					'width': 300,
					'panel_config': {
						'height': 250
					}
				},
				'channelTip': {
					'data': LANG('标记广告位所用，譬如“网站联盟”可以算一个渠道。不影响广告投放')
				}
			})
		},
		setData: function( data ) {
			this.$data = data;
			this.$.channel.setValue(data.AdSubChannelId);
		},
		getData: function() {
			var val = this.$.channel.getValue();
			return {
				'_id': this.$data['_id'],
				'AdSubChannelId': val['id'],
				'Name': this.vm.getItem('mediaName')
			}
		},
		reset: function() {
			this.vm.reset();
			this.$.channel.reset();
			return this;
		},
		// 点击完成
		onOk: function() {
			var data = this.getData();
			pubjs.mc.send(
				this.getConfig('url'),
				data,
				this.onSave.bind( this )
			)
			return false;
		},
		onSave: function( err, data ) {
			if( err ) {
				pubjs.alert(err.message);
				return;
			}
			this.fire('mediaSaveSuccess');
			this.hide().reset();
			return false;
		}

	});
	exports.editMediaSpot = EditMediaSpot;

	// 编辑广告位 TODOadsBox模块有bug by TBC
	var EditSpot = popwin.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'width': 510,
				'height': 480,
				'silence': false,
				'url': 'spot/update'
			});
			this.Super('init', arguments);
		},

		afterBuild: function() {
			this.Super('afterBuild', arguments);
			var el = this.getDOM('body');
			el.addClass('editMediaSpot');
			this.setTitle(LANG('编辑广告位'));
			el.append('<div pub-name="editBody" pub-mod="ads/adsBox.main"/>');
			this.buildFromTemplate({
				'editBody': {
					'isShowChannel': false,
					'class': 'P-adsAddCon'
				}
			});
		},
		setData: function( data ) {
			this.$spotId = data._id || null;
			this.$.editBody.setData(data);
		},

		onOk: function() {
			var data = this.$.editBody.getData();
			data._id = this.$spotId;
			pubjs.mc.send(
				this.getConfig('url'),
				data,
				this.onSave.bind(this)
			);
			return false;
		},
		onSave: function( err, data ) {
			if( err ) {
				pubjs.alert(err.message);
				return false;
			}
			this.fire('spotUpdateSuccess');
			this.hide();
		},
		onCancel: function(){
			this.$.editBody.reset();
			this.hide();
		}
	});
	exports.editSpot = EditSpot;

	// 编辑/新建频道 by TBC
	var AEChannel = popwin.extend({
		init: function(config) {
			config = pubjs.conf(config, {
				'width': 400,
				// 'height': 200,
				'urlAdd': 'massChannel/add',
				'urlUpdate': 'massChannel/update',
				'view_model': {
					'channelName': ''
				}
			});
			this.$actionType = {
				'edit': LANG('编辑'),
				'add': LANG('添加')
			}
			this.Super('init', arguments);
		},

		afterBuild: function() {
			this.Super('afterBuild', arguments);
			var el = this.getDOM('body');

			el.addClass('editMediaSpot');
			this.setTitle(this.$actionType['add'] + LANG('频道'));
			el.append([
				'<span class="dib pt10 pb10 mr15">'+ LANG('频道名称:') +'</span>',
				'<input type="text" ms-duplex="channelName">'
			].join('')).addClass('uk-form');
		},

		setData: function( data ) {
			var action = this.$action = data['action'];
			var name = data['Name'];
			this.$MassMediaId = data.MassMediaId;
			this.$id = +data['_id'] || null;
			this.setTitle(this.$actionType[action] + LANG('频道'));
			this.vm.setItem('channelName', name || "");
		},

		getData: function() {
			return {
				'_id': this.$id || null,
				'MassMediaId': this.$MassMediaId,
				'Name': this.vm.getItem('channelName')
			}
		},

		// 点击完成
		onOk: function() {
			var data = this.getData();
			var C = this.getConfig();
			var url = this.$action === 'add' ? C.urlAdd : C.urlUpdate;

			pubjs.mc.send(
				url,
				data,
				this.onSave.bind( this )
			)
			return false;
		},
		onSave: function( err, data ) {
			if( err ) {
				pubjs.alert(err.message);
				return;
			}
			this.fire('channelSaveSuccess');
			this.hide().reset();
			return false;
		}
	});
	exports.aeChannel = AEChannel;
});